#include "message.h"
#include <chrono>
#include <zlib.h>

uint64_t get_timestamp() {
    using namespace std::chrono;
    return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}

uint32_t compute_checksum(const MessageHeader& header, const uint8_t* body, size_t body_len) {
    // 使用zlib的crc32函数计算校验和
    uLong crc = crc32(0L, Z_NULL, 0);
    
    // 计算头部的校验和（checksum字段置0）
    MessageHeader temp = header;
    temp.checksum = 0;
    crc = crc32(crc, reinterpret_cast<const Bytef*>(&temp), sizeof(MessageHeader));
    printf("headcrc = %u\n", crc);
    // 计算消息体的校验和
    if (body && body_len > 0) {
        crc = crc32(crc, reinterpret_cast<const Bytef*>(body), body_len);
        printf("bodycrc = %u\n", crc);
    }
    
    return static_cast<uint32_t>(crc);
}
